package vn.sunnet.backviet01.game.coloa.main;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import vn.sunnet.util.ui.SplashView;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.PackageManager.NameNotFoundException;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
//import android.view.animation.Animation;
//import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

public class SplashScreen extends Activity {
	public static int SPLASH_TIME = 3200;
	public static int MSG_START_ANIM = 0;
	public static int MSG_STOP_ANIM = 1;

//	private Animation slide;
	private FrameLayout mlayout;
	private SplashView mView;
	private Handler hRefresh;
	private boolean blnIsStopAnim = false;

	/**
	 * On create
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		// if (qplay_sound == null)
		// qplay_sound = new Audio(this, R.raw.backgournd_sound);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// qplay_sound.play();z
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		setContentView(R.layout.splash);

		// init animation
		mlayout = (FrameLayout) findViewById(R.id.splash_view);
		mView = new SplashView(this);
		mView.setPartnerLogoID(R.drawable.partner_logo);
		mlayout.addView(mView);

//		slide = AnimationUtils.loadAnimation(this, R.anim.animation);
		blnIsStopAnim = false;

		// // Create handler
		hRefresh = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					/* Refresh UI */
					runAnimation();
					break;
				case 1:
					stopAnimation();
				}
			}
		};

		// post msg for handle
		hRefresh.sendEmptyMessage(MSG_START_ANIM);
		hRefresh.sendEmptyMessageDelayed(MSG_STOP_ANIM, SPLASH_TIME);
		
		try {
			final String packageName = this.getPackageName();
			
		    PackageInfo info = this.getPackageManager().getPackageInfo(
		    		packageName, PackageManager.GET_SIGNATURES);
		    for (Signature signature : info.signatures) 
		        {
		           MessageDigest md = MessageDigest.getInstance("SHA");
		           md.update(signature.toByteArray());
		           Log.e("keyhash: ", "keyhash: " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
		        }
		    }
		 catch (NameNotFoundException e) {
		} catch (NoSuchAlgorithmException e) {
		}
	}

	/**
	 * Run animation
	 */
	public void runAnimation() {
		blnIsStopAnim = false;
		mView.startAnim();
		// mlayout.startAnimation(slide);
	}

	/**
	 * Stop animation
	 */
	public void stopAnimation() {
		// mView
		if (blnIsStopAnim)
			return;
		mView.stopAnim();
		blnIsStopAnim = true;
		Intent i;

		i = new Intent(SplashScreen.this, Guide.class);
		startActivity(i);
		finish();
	}

	/**
	 * Stop animation on touch event
	 */
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN)
			stopAnimation();
		return true;
	}
}
